import *as UmfDialog from '../../../../utils/UmfDialog.js';
import network_utils from '../../../../http/NetworkUtils';
import constants from '../../../../common/Constants';
import * as UserUtils from '../../../../model/UserUtils';
import * as UmfToast from '../../../../utils/UmfToast';

var app = getApp();
var UserId = "";
Page({

    /**
     * 页面的初始数据
     */
    data: {},

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        UserId = options.userId;
        this.requestPermConf();
    },

    /**
     * 请求权限
     */
    requestPermConf() {
        let params = {
            userId: UserId,
        };

        network_utils.buildRequest(constants.Interfaces.FUNC_QUERY_PERM_CONF, params, {
            doHttpSucess: (response) => {
                if (constants.RetCodes.SUCCESS === response.retCode) {
                    let permissionConf = response["permissionConf"];
                    this.setData({
                        isSuccess: true,
                        canSearchStores: app.globalData.userData.permConfig.canSearchStores(),
                        canSearchProfit: app.globalData.userData.permConfig.canSearchProfit(),
                        searchStorePerm: permissionConf.indexOf("searchStorePerm") >= 0,
                        searchProfitPerm: permissionConf.indexOf("searchProfitPerm") >= 0,
                        fetchBillDocPerm: permissionConf.indexOf("fetchBillDocPerm") >= 0,
                    });
                    return true;
                }
                return false;
            }
        }).send();
    },

    /**
     * 修改权限
     * @param e
     */
    changeLimits: function (e) {
        let params = {
            userId: UserId,
            permissionConf: new UserUtils.permConfig().getConfArray(e.detail.value.searchProfitPerm, e.detail.value.searchStorePerm, e.detail.value.fetchBillDocPerm)
        };
        network_utils.buildRequest(constants.Interfaces.FUNC_SALEMAN_LIMIT_CONF, params, {
            doHttpSucess: (response) => {
                if (constants.RetCodes.SUCCESS === response.retCode) {
                    UmfToast.showToast("权限已成功修改");
                    setTimeout(()=> {
                        wx.navigateBack({
                            delta: 2
                        })
                    }, 1000);
                    return true
                }
                return false
            }
        }).send();
    }
});